const app = getApp()
console.log(app.globalData.user)
Page({
  data:{
    headHeight:40,
    chatListHeight:'0',
    keyboardHeight:0,
    allheight:app.globalData.sysHeight,
    inutPanelHeight:0.16*app.globalData.sysHeight,
    messageList:[],
    curMessage:"",
    receiveId:'',
    senduser:'',
    sendId:'',
    receiveuser:{
      username:'',
      image:''
    },
    flag:true,
    findindex:'',
    findflag:0,
    isStranger:''
  },
  // 点击头像跳转主页
  gotoHome(){
    wx.navigateTo({
      url: `/pages/other/other?findid=${this.data.receiveId}`,
    })
  },
  // 跳转到自己的主页
  gotomyselfHome(){
    wx.navigateTo({
      url: `/pages/other/other?findid=${this.data.sendId}`,
    })
  },
  gotosetting(){
    wx.navigateTo({
      url: `/pages/setting/setting?id=${this.data.receiveId}`,
    })
    console.log(this.data.receiveuser)
  },
  // 定位到最新消息
  getNewChat(){
    this.setData({
      toView: `item${this.data.messageList.length - 1}`,
    })
  },
  //查找定位聊天记录
  getfindchat(index){
    this.setData({
      toView: `item${index}`,
    })
  },
  // 更改消息列表高度(依据是否有键盘)
  setChatListHeight() {
    this.setData({
      chatListHeight: app.globalData.sysHeight - this.data.headHeight - this.data.keyboardHeight- 1.3*this.data.inutPanelHeight
    })
  },
  // 点击对话列表隐藏键盘
  hideKeyboard(){
    wx.hideKeyboard();
    // this.hideMediaPanel();
    this.setChatListHeight();
  },
  /**
   * 生命周期函数--监听页面加载
  */
  onLoad(options){
    this.setData({
      receiveId:options.receiveId,
      senduser:app.globalData.user,
      sendId:app.globalData.user.id,
      findflag:options.findflag
    })
    if(this.data.findflag==1){
      this.setData({
        findindex:options.findindex
      })
    }
    this.setChatListHeight();
    wx.onKeyboardHeightChange(res => { //监听键盘高度变化
      this.setData({
        keyboardHeight: res.height
      });
      this.getNewChat();
      this.setChatListHeight();
    });
  },
  /**
   * 生命周期函数receiveid--监听页面显示
   */
  onShow() {
    // 获取接收者信息
    wx.request({
      url: `http://localhost:3000/message/getreceiveid?receiveid=${this.data.receiveId}`,
      method:'GET',
      success:res=>{
        this.setData({
          receiveuser:res.data.user
        })
        console.log(this.data.senduser,this.data.receiveuser)
      },
      fail:err=>{
        console.error(err)
      }
    })
    // 获取消息列表
    wx.request({
      url: `http://localhost:3000/message/gettalks?receiveid=${this.data.receiveId}&sendid=${this.data.sendId}`,
      method:'GET',
      success:res=>{
        console.log(res.data.list)
        this.setData({
          messageList:res.data.list
        })
        if(this.data.findflag==1){
          this.getfindchat(this.data.findindex)
        }else{
          this.getNewChat()
        }
      },
      fail:err=>{
        console.error(err)
      }
    })
    this.setChatListHeight();
    wx.onKeyboardHeightChange(res => { //监听键盘高度变化
      this.setData({
        keyboardHeight: res.height
      });
      this.getNewChat();
      this.setChatListHeight();
    });
    
  },
  // 输入框绑定内容
  getInput(e){
    let value = e.detail.value;
    this.setData({
      curMessage: value
    });
  },
  // 发送消息
  send() {
    let messageList = this.data.messageList;
    let curMessage = this.data.curMessage;
    let date = new Date()
    console.log(date)
    if (curMessage.trim() === "") {
      wx.showToast({
        title: '请输入聊天内容',
        duration: 2000,
        icon: "none"
      })
      return;
    }
    wx.request({
      url: `http://localhost:3000/message/muattention?id=${this.data.sendId}&receiveid=${this.data.receiveId}`,
      method:'GET',
      success:res=>{
        if(res.data.code==200){
          this.setData({
            isStranger:0
          })
        }else{
          this.setData({
            isStranger:1 //没有互相关注
          })
        }
        console.log(this.data.isStranger)
        // 发送添加请求
        let obj = {
          sendid:this.data.sendId,
          receiveid:this.data.receiveId,
          content:curMessage,
          time:date,
          stranger:this.data.isStranger
        }
        console.log(obj)
        wx.request({
          url: 'http://localhost:3000/message/addmessage',
          method:"POST",
          data:{
            messageobj:obj
          },
          header:{
            'content-type':'application/json'
          },
          success:res=>{
            console.log(res.data)
            if(res.data.code==200){
              messageList.push(obj); //添加到消息列表
              this.setData({
                curMessage:"",
                messageList:messageList
              })
              this.getNewChat() //定位到最新消息
            }else{
              wx.showToast({
                title: '发送失败',
                duration: 2000,
                icon: "none"
              })
              this.setData({
                curMessage:"",
              })
            }
          },
          fail:err=>{
            console.error(err)
          }
        })
      },
      fail:err=>{
        console.error(err)
      }
    })
  },
})